/*
 * @Author: 江舟 jijiangzhou@recycloud.cn
 * @Date: 2024-04-02 15:11:37
 * @LastEditors: 江舟 jijiangzhou@recycloud.cn
 * @LastEditTime: 2024-04-19 11:11:42
 * @FilePath: \vite-project\src\components\common\AreaSelect.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import CITY_LIST from 'constant/CityArrays';
import { Cascader } from 'antd';

interface AreaSelectProps {
    onChange?: (val: any) => void;
    value?: any[];
    placeholder?: string;
}

const AreaSelect: React.FC<AreaSelectProps> = ({ value = [], onChange, placeholder = '请选择地区' }) => {
    const onChangeValue = (value: string | any[]) => {
        if (!value) {
            onChange?.(value);
        } else {
            let [province, city, country] = value;
            if (value?.length === 2) {
                country = city;
                city = province;
            }
            onChange?.([province, city, country]);
        }
    };
    return (
        <Cascader
            value={value}
            options={CITY_LIST}
            fieldNames={{ label: 'label', value: 'label' }}
            placeholder={placeholder}
            dropdownMenuColumnStyle={{
                minWidth: 200,
            }}
            onChange={onChangeValue}
        />
    );
};

export default AreaSelect;
